#include <poker.h>

//点击手中的牌
void mouse_hand(int x, int y, struct tractor_t *tractor)
{
    int w, h, id, sx, sy;

    sx = (DESKTOP_W - (tractor->player[0].hand_num * (CARD_SPACE - 1) + CARD_W)) / 2;
    sy = DESKTOP_H - CARD_H - MARGIN - CARD_SPACE;

    w = (tractor->player[0].hand_num - 1) * CARD_SPACE + CARD_W;
    h = CARD_H + CARD_SPACE;

    if (x >= sx && x <= sx + w && y >= sy && y <= sy + h)
    {
        id = (x - sx) / CARD_SPACE;

        if (id >= tractor->player[0].hand_num)
            id = tractor->player[0].hand_num - 1;

        id += (CARD_PLAYER - CARD_HAND);

        /*
         *printf("x = %d, y = %d, sx = %d, sy = %d, w = %d, h = %d, id = %d, num = %d\n",
         *            x, y, sx, sy, w, h, id, tractor->player[0].hand_num);
         */

        if ((y < sy + CARD_SPACE && 
                !tractor->poker[tractor->player[0].card[id]].selection) ||
            (y > sy + h - CARD_SPACE && 
                tractor->poker[tractor->player[0].card[id]].selection))
            return;

        tractor->poker[tractor->player[0].card[id]].selection = 
                    !tractor->poker[tractor->player[0].card[id]].selection;

        show_tractor(tractor);
    }
}

//点击桌面上的牌
void mouse_desktop(int x, int y, struct tractor_t *tractor)
{
    int w, h, id, sx, sy, row, row_step, tx;

    sx = (DESKTOP_W - ((CARD_W + CARD_BORDER_SPACE) * CARD_ROW + (CARD_SPACE - CARD_BORDER_SPACE) * (CARD_ROW - 1))) / 2;
    sy = DESKTOP_H - MARGIN - CARD_H * 3 - CARD_SPACE * 5;

    w = CARD_ROW * (CARD_W + CARD_BORDER_SPACE) + (CARD_SPACE - CARD_BORDER_SPACE) * (CARD_ROW - 1);
    h = CARD_H * 2 + CARD_SPACE * 3;


    if (x >= sx && x <= sx + w && y >= sy && y <= sy + h)
    {
        row_step = CARD_H + CARD_SPACE * 2;

        tx = (x - sx) / (CARD_W + CARD_SPACE);
        row = (y - sy) / (CARD_H + CARD_SPACE + CARD_SPACE / 2);

        id = tx + CARD_ROW * 2;

        if (row == 1)
            id += CARD_ROW;

        if (tractor->player[0].card[id] == NONE)
            id -= CARD_DESKTOP / 2;
            
        /*printf("id = %d, row = %d, w = %d, h = %d, sx = %d, sy = %d\n", id, row, w, h, sx, sy);*/

        if (tractor->player[0].card[id] == NONE ||
            x < sx + tx * (CARD_W + CARD_SPACE) || 
            x > sx + tx * (CARD_W + CARD_SPACE) + CARD_W ||
            (((y < sy + row * row_step + CARD_SPACE) || 
                    (y > sy + row * row_step + CARD_H + CARD_SPACE)) && 
                !tractor->poker[tractor->player[0].card[id]].selection) ||
            (((y < sy + row * row_step) ||
                    (y > sy + row * row_step + CARD_H)) &&
                tractor->poker[tractor->player[0].card[id]].selection))
                return;

        tractor->poker[tractor->player[0].card[id]].selection = 
                    !tractor->poker[tractor->player[0].card[id]].selection;
        show_tractor(tractor);
    }
}

void cancel_handle(void *data)
{
    int i;
    struct tractor_t *tractor = (struct tractor_t *)data;

    for (i = 0; i < CARD_PLAYER; i++)
    {
        if (tractor->player[0].card[i] != NONE && 
                tractor->poker[tractor->player[0].card[i]].selection)
            tractor->poker[tractor->player[0].card[i]].selection = 0;
    }
    /*
     *shuffle(tractor->poker);
     *tractor->who = -1;
     *tractor->lord = -1;
     *deal_card(tractor);
     */
    show_tractor(tractor);
}

void bottom_handle(void *data)
{
    int i, c = 0, j;
    struct tractor_t *tractor = (struct tractor_t *)data;

    for (i = 0; i < CARD_LORD_PLAYER; i++)
    {
        if (tractor->poker[tractor->player[tractor->who].card[i]].selection)
            c++;
    }
    if (c != CARD_BOTTOM)
        return;

    for (i = 0, j = 0; i < CARD_LORD_PLAYER - j; i++)
    {
        if (tractor->poker[tractor->player[tractor->who].card[i]].selection)
        {
            tractor->poker[tractor->player[tractor->who].card[i]].selection = 0;
            tractor->bottom[j++] = tractor->player[tractor->who].card[i];
            tractor->player[tractor->who].card[i] = NONE;
            memmove(tractor->player[tractor->who].card + i, 
                        tractor->player[tractor->who].card + i + 1, CARD_LORD_PLAYER - i - 1);
            i--;
        }
    }

    tractor->player[tractor->who].hand_num -= CARD_BOTTOM;
    tractor->ck_bottom = 1;
    show_tractor(tractor);
}

void lord_handle(void *data)
{

}

//点击出牌按钮
void sell_handle(void *data)
{
    struct tractor_t *tractor = (struct tractor_t *)data;
    unsigned char card[CARD_SELL];
    int sell_num = 0, i, j, hide_num = CARD_DESKTOP / 2;

    for (i = 0; i < CARD_PLAYER; i++)
    {
        if (tractor->player[0].card[i] != NONE && 
                tractor->poker[tractor->player[0].card[i]].selection)
            card[sell_num++] = tractor->player[0].card[i];
    }

    if (sell_rule(tractor, card, sell_num) == -1)
        return;

    for (i = 0, tractor->desktop_num = 0, j = 0; i < CARD_DESKTOP + tractor->player[0].hand_num; i++)
    {
        if (tractor->player[0].card[i] != NONE && 
                tractor->poker[tractor->player[0].card[i]].selection)
        {
            tractor->desktop[j++] = tractor->player[0].card[i];
            tractor->desktop_num++;
            tractor->player[0].card[i] = NONE;
            if (i >= CARD_PLAYER - CARD_HAND)
            {
                tractor->player[0].hand_num--;
                memmove(tractor->player[0].card + i, 
                        tractor->player[0].card + i + 1, 
                        CARD_PLAYER - i - 1);
                i--;
            }
            else if (i >= hide_num)
            {
                tractor->poker[tractor->player[0].card[i - hide_num]].hide = 0;
            }
        }
    }

    show_tractor(tractor);
}

void mouse_handle(int button, int x, int y, void *data)
{
    struct tractor_t *tractor = (struct tractor_t *)data;

    /*mouse_button(x, y, tractor);*/
    mouse_hand(x, y, tractor);
    mouse_desktop(x, y, tractor);
}

